package org.eclipse.iota.xtext.ui.highlighting;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.iota.model.Reference;
import org.eclipse.iota.model.Variable;
import org.eclipse.xtext.RuleCall;
import org.eclipse.xtext.parsetree.AbstractNode;
import org.eclipse.xtext.parsetree.NodeUtil;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightedPositionAcceptor;
import org.eclipse.xtext.ui.editor.syntaxcoloring.ISemanticHighlightingCalculator;

/**
 * Semantic highlighting for Iota.
 * @author Christian Krause
 * @generated NOT
 */
public class IotaSemanticHighlightingCalculator implements
		ISemanticHighlightingCalculator {
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.xtext.ui.editor.syntaxcoloring.ISemanticHighlightingCalculator#provideHighlightingFor(org.eclipse.xtext.resource.XtextResource, org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightedPositionAcceptor)
	 */
	@Override
	public void provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor) {
		
		// Resource must be set.
		if (resource==null) {
			return;
		}
		
		// Iterate over all nodes in the parse tree.
		Iterable<AbstractNode> allNodes = NodeUtil.getAllContents(resource.getParseResult().getRootNode());
		for (AbstractNode node : allNodes) {
			EObject elem = node.getElement();
			
			if (elem instanceof Variable || 
			   (elem instanceof Reference && ((Reference) elem).getVariable()!=null)) {
				acceptor.addPosition(node.getOffset(), node.getLength(), 
						IotaHighlightingConfiguration.VARIABLE);
			} else {
				elem = node.getGrammarElement();
				if (elem instanceof RuleCall && "Rule".equals(((RuleCall) elem).getRule().getName())) {
					acceptor.addPosition(node.getOffset(), node.getLength(), 
							IotaHighlightingConfiguration.RULE);					
				}
			}
		}		
	}

}
